package com.demo.mantis.web;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.demo.mantis.model.Book;
import com.demo.mantis.service.HelloService;
import cn.techarts.dragonfly.web.WebContext;
import cn.techarts.dragonfly.web.rest.Get;
import cn.techarts.dragonfly.web.rest.Post;
import cn.techarts.dragonfly.web.rest.Restful;

@Singleton
@Restful
public class HelloWebService {
	
	@Inject
	private HelloService service;
	
	@Get("/hello/{id}")
	public String hello(WebContext arg) {
		var id = arg.intAt(0);
		var result = service.hello(id, "How are you?");
		if(result == null) {
			arg.error(-2, "The friend does not exist");
		}
		return result;
	}
	
	@Post("/book/{isbn}/{name}/{author}/{price}")
	public Book buyBook(WebContext arg) {
		var isbn = arg.at(0);
		var name = arg.at(1);
		var author = arg.at(2);
		var price = arg.floatAt(3);
		var press = arg.get("press");
		var book = new Book(isbn, name, author, price);
		book.setPress(press);
		return service.buyBook(book);
	}
	
	@Get(value="/demo/token", mandatory=false)
	public String generateToken(WebContext arg) {
		return arg.getToken("rocwon@foxmail.com");
	}
}
